package com.anteater.eattheant.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import android.content.Context;

import com.anteater.eattheant.R;

public class Level implements Serializable {

	private static final long serialVersionUID = 3065366754195585939L;

	private static final int LEVEL[] = { R.raw.level00, R.raw.level01,
			R.raw.level02, R.raw.level03, R.raw.level04, R.raw.level05,
			R.raw.level06, R.raw.level07, R.raw.level08, R.raw.level09,
			R.raw.level10, R.raw.level11, R.raw.level12, R.raw.level13,
			R.raw.level14 };

	private int level;
	private short graph[][];
	private List<Ant> ants;
	private List<Escape> escapes;
	private short score;
	
	private String description;
	private String title;
	
	public Level(Context ctx, int level) {
		this.level = level;

		BufferedReader br = new BufferedReader(new InputStreamReader(ctx
				.getResources().openRawResource(LEVEL[level - 1])));

		try {
			String line = null;
			StringTokenizer tokenizer = null;

			// STYLE
			line = br.readLine();

			// TITLE
			title = br.readLine();
			
			// DESCRIPTION
			description = br.readLine();

			line = br.readLine();
			tokenizer = new StringTokenizer(line);

			int height = new Integer(tokenizer.nextToken());
			int width = new Integer(tokenizer.nextToken());

			graph = new short[height][width];

			for (int i = 0; i < height; i++) {
				line = br.readLine();
				tokenizer = new StringTokenizer(line);
				for (int j = 0; j < width; j++) {
					graph[i][j] = new Short(tokenizer.nextToken());
				}
			}

			line = br.readLine();
			tokenizer = new StringTokenizer(line);
			int numAnts = new Integer(tokenizer.nextToken());
			ants = new ArrayList<Ant>(numAnts);

			for (int i = 0; i < numAnts; i++) {
				line = br.readLine();
				tokenizer = new StringTokenizer(line);
				short y = new Short(tokenizer.nextToken());
				short x = new Short(tokenizer.nextToken());
				ants.add(new Ant(y, x));
			}

			line = br.readLine();
			tokenizer = new StringTokenizer(line);
			int numEscapes = new Integer(tokenizer.nextToken());
			escapes = new ArrayList<Escape>(numEscapes);

			for (int i = 0; i < numEscapes; i++) {
				line = br.readLine();
				tokenizer = new StringTokenizer(line);
				short y = new Short(tokenizer.nextToken());
				short x = new Short(tokenizer.nextToken());
				escapes.add(new Escape(y, x));
			}

			line = br.readLine();
			tokenizer = new StringTokenizer(line);
			score = new Short(tokenizer.nextToken());

			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getLevel() {
		return level;
	}

	public short[][] getGraph() {
		return graph;
	}

	public List<Ant> getAnts() {
		return ants;
	}

	public List<Escape> getEscapes() {
		return escapes;
	}

	public short getScore() {
		return score;
	}

	public String getDescription() {
		return description;
	}
	
	public String getTitle() {
		return title;
	}
}
